package user;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import math.Vector2D;

public class DropDownMenu
{
	private static Color text, back, lines, hover;
	
	public static void setLookAndFeel( Color t, Color l, Color b, Color h )
	{
		text = t; lines = l; back = b; hover = h;
	}
	
	private final int width = 200;
	
	private Vector2D pos;
	private String[] options;
	private int hoverOption;
	
	public DropDownMenu( Vector2D p, String[] options )
	{
		this.pos = p;
		this.options = options;
		
		hoverOption = -1;
	}
	
	public void render( Graphics2D g )
	{
		g.setColor( back );
		g.fillRect( (int)pos.getX(), (int)pos.getY(), width, options.length * 20 );
		
		g.setColor( lines );
		g.drawRect( (int)pos.getX(), (int)pos.getY(), width, options.length * 20 );
		
		for( int i = 0; i < options.length; i++ )
    	{
			g.setColor( lines );
			g.drawLine( (int)pos.getX(), (int)pos.getY() + i * 20, (int)pos.getX() + width, (int)pos.getY() + i * 20 );
			
			g.setColor( text );
    		g.drawString( options[i], (int)pos.getX() + 20, (int)pos.getY() + i * 20 + 15 );
    	}
		
		if( hoverOption != -1 )
		{
			g.setColor( hover );
			g.fillRect( (int)pos.getX(), (int)pos.getY() + hoverOption * 20, width, 20 );
		}
	}
	
	public void setHover( int i )
	{
		hoverOption = i;
	}
	
	
	/**
	 * @param v mouse location
	 * @return the location of the option being hovered over, -1 if none of them
	 */
	public int mouseOver( Vector2D v )
	{
		for( int i = 0; i < options.length; i++ )
    	{
    		if( ( new Rectangle( (int)pos.getX(), (int)pos.getY() + i * 20, width, 20 ) ).contains( v.getX(), v.getY() ) )
    		{
    			return i;
    		}
    	}
		
		return -1;
	}
	
	/**
     * @param v mouse location
     * @return null if the mouse does not contact any option, returns the option selected otherwise
     */
    public String click( Vector2D v )
    { 
    	for( int i = 0; i < options.length; i++ )
    	{
    		if( ( new Rectangle( (int)pos.getX(), (int)pos.getY() + i * 20, width, 20 ) ).contains( v.getX(), v.getY() ) )
    		{
    			return options[i];
    		}
    	}
    	
    	return null;
    }
}
